<template>
  <div class="profile-container">
    <el-container>
      <el-header class="header">
        <div class="header-content">
          <div class="header-left">
            <h1 class="page-title">用户资料</h1>
          </div>
          <div class="header-right">
            <el-button @click="goBack">
              <el-icon><ArrowLeft /></el-icon>
              返回
            </el-button>
          </div>
        </div>
      </el-header>
      
      <el-main class="main-content">
        <div class="profile-content">
          <el-card class="profile-card">
            <div class="profile-header">
              <div class="avatar-section">
                <el-avatar :size="80" :src="userStore.userAvatar" />
                <el-button type="primary" size="small" @click="uploadAvatar">
                  <el-icon><Camera /></el-icon>
                  更换头像
                </el-button>
              </div>
              <div class="user-info">
                <h2>{{ userStore.userName }}</h2>
                <p class="user-email">{{ userStore.currentUser?.email }}</p>
              </div>
            </div>
            
            <el-divider />
            
            <div class="profile-body">
              <el-descriptions title="基本信息" :column="2" border>
                <el-descriptions-item label="用户名">
                  {{ userStore.currentUser?.username }}
                </el-descriptions-item>
                <el-descriptions-item label="昵称">
                  {{ userStore.currentUser?.nickname || '未设置' }}
                </el-descriptions-item>
                <el-descriptions-item label="邮箱">
                  {{ userStore.currentUser?.email }}
                </el-descriptions-item>
                <el-descriptions-item label="性别">
                  {{ userStore.currentUser?.gender || '未设置' }}
                </el-descriptions-item>
                <el-descriptions-item label="地区">
                  {{ userStore.currentUser?.location || '未设置' }}
                </el-descriptions-item>
                <el-descriptions-item label="个人简介">
                  {{ userStore.currentUser?.bio || '未设置' }}
                </el-descriptions-item>
                <el-descriptions-item label="注册时间">
                  {{ formatDate(userStore.currentUser?.created_at) }}
                </el-descriptions-item>
                <el-descriptions-item label="账户状态">
                  <el-tag :type="userStore.currentUser?.is_active ? 'success' : 'danger'">
                    {{ userStore.currentUser?.is_active ? '正常' : '禁用' }}
                  </el-tag>
                </el-descriptions-item>
              </el-descriptions>
            </div>
            
            <div class="profile-actions">
              <el-button type="primary" @click="editProfile">
                <el-icon><Edit /></el-icon>
                编辑资料
              </el-button>
              <el-button @click="goToPreferences">
                <el-icon><Setting /></el-icon>
                偏好设置
              </el-button>
            </div>
          </el-card>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../store/modules/user'
import { ArrowLeft, Camera, Edit, Setting } from '@element-plus/icons-vue'
import ThemeToggle from '../components/common/ThemeToggle.vue'
import { getRouteHistory } from '../router'

const router = useRouter()
const userStore = useUserStore()

onMounted(async () => {
  if (!userStore.currentUser) {
    await userStore.fetchCurrentUser()
  }
})

const goBack = () => {
  const history = getRouteHistory()
  const validRoutes = history.filter(route => 
    !['/login', '/register', '/404', '/'].includes(route) &&
    !route.includes('error')
  )
  
  if (validRoutes.length > 0) {
    const lastRoute = validRoutes[validRoutes.length - 1]
    
    // 如果返回的是/main页面，需要恢复Main页面的状态
    if (lastRoute === '/main') {
      // 检查是否有保存的Main页面状态
      const savedState = localStorage.getItem('mainPageState')
      if (savedState) {
        // 有保存的状态，直接跳转到/main，状态会在Main.vue中恢复
        router.push('/main')
        return
      }
    }
    
    router.push(lastRoute)
  } else {
    router.push('/main')
  }
}

const uploadAvatar = () => {
  // TODO: 实现头像上传功能
  console.log('上传头像')
}

const editProfile = () => {
  // TODO: 跳转到编辑资料页面
  console.log('编辑资料')
}

const goToPreferences = () => {
  router.push('/preferences')
}

const formatDate = (dateString?: string) => {
  if (!dateString) return '未知'
  return new Date(dateString).toLocaleDateString('zh-CN')
}
</script>

<style scoped>
.profile-container {
  min-height: 100vh;
  background-color: var(--background);
  transition: background-color 0.3s ease;
}

.header {
  background: var(--surface);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: background-color 0.3s ease;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.page-title {
  margin: 0;
  font-size: 24px;
  color: var(--text-primary);
}

.main-content {
  padding: 20px;
}

.profile-content {
  max-width: 800px;
  margin: 0 auto;
}

.profile-card {
  border-radius: 12px;
}

.profile-header {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-bottom: 20px;
}

.avatar-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.user-info h2 {
  margin: 0 0 8px 0;
  font-size: 24px;
  color: #333;
}

.user-email {
  margin: 0;
  color: #666;
  font-size: 14px;
}

.profile-body {
  margin: 20px 0;
}

.profile-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-top: 20px;
}
</style> 